<script setup>

import {getCurrentInstance, ref} from "vue";
import {operateLogStatusList, operateTypeList} from "@/enum/enum";
import {enumToArray} from "@/utils/utils";
import {clearSysOperLog, deleteSysOperLog, listSysOperLog} from "@/api/system/log/oper_log";
import OperDetailForm from "@/views/log/OperDetailForm.vue";

const {proxy} = getCurrentInstance();
const sysOperLogList = ref([])
const total = ref(0)
const queryFormRef = ref();
const queryForm = ref({
  pageNo: 1,
  pageSize: 10,
  operLocation: null,
  title: null,
  operName: null,
  operType: null,
  status: null,
  timeArr: null,
})
const ids = ref([])
const operLogStatusOptions = enumToArray(operateLogStatusList);
const operTypeOptions = enumToArray(operateTypeList);
const operDetailFormRef = ref();

const getList = async () => {
  const {data} = await listSysOperLog(queryForm.value);
  total.value = data.total;
  sysOperLogList.value = data.list;
}

const handleQuery = () => {
  queryForm.value.pageNo = 1;
  getList();
}

const handleReset = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

const handleSelectionChange = (selection) => {
  ids.value = selection.map(item => item.operId);
}

const handleDelete = () => {
  const logId = ids.value;
  proxy.$modal.confirm(`确定删除选中的操作日志吗？`).then(async () => {
    await deleteSysOperLog(logId);
    proxy.$modal.okNotify('删除成功');
    await getList();
  })
}

const handleClear = () => {
  proxy.$modal.confirm(`确定清空操作日志吗？`).then(async () => {
    await clearSysOperLog();
    proxy.$modal.okNotify('清空成功');
    await getList();
  })
}

const handleDetail = (operId) =>{
  operDetailFormRef.value.open(operId)
}

getList()

</script>

<template>
  <div class="app-container">

    <a-form :model="queryForm" ref="queryFormRef" layout="inline" class="demo-form-inline">
      <a-form-item label="操作地址" name="operLocation">
        <a-input v-model:value="queryForm.operLocation" placeholder="请输入操作地址"/>
      </a-form-item>
      <a-form-item label="系统模块" name="title">
        <a-input v-model:value="queryForm.title" placeholder="请输入系统模块"/>
      </a-form-item>
      <a-form-item label="操作人员" name="operName">
        <a-input v-model:value="queryForm.operName" placeholder="请输入操作人员"/>
      </a-form-item>
      <a-form-item label="操作类型" name="operType">
        <a-select v-model:value="queryForm.operType" placeholder="请选择操作类型">
          <a-select-option v-for="(item, index) in operTypeOptions" :key="index" :label="item.label"
                           :value="item.value">{{ item.label }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="操作状态" name="status">
        <a-select v-model:value="queryForm.status" placeholder="请选择操作状态">
          <a-select-option v-for="(item, index) in operLogStatusOptions" :key="index" :label="item.label"
                           :value="item.value">{{ item.label }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="登录时间" name="timeArr">
        <a-range-picker
            v-model:value="queryForm.timeArr"
            show-time
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
        />
      </a-form-item>
      <a-form-item>
        <a-space wrap>
          <a-button type="primary" @click="handleQuery" class="flex items-center mr-2">
            <icon icon="ep-search" size="14px" color="unset" class="mr-1"/>
            查询
          </a-button>
          <a-button plain @click="handleReset" class="flex items-center">
            <icon icon="ep-refresh" size="14px" color="unset" class="mr-1"/>
            重置
          </a-button>
        </a-space>
      </a-form-item>
    </a-form>

    <a-space wrap class="!mb-4 !mt-4">
      <a-button danger plain :disabled="!ids.length" class="flex items-center"
                @click="handleDelete" v-hasPermission="['system:oper_log:delete']">
        <icon icon="ep-delete" size="14px" color="unset" class="mr-1"/>
        删除
      </a-button>
      <a-button danger plain class="flex items-center" @click="handleClear"
                v-hasPermission="['system:oper_log:clear']">
        <icon icon="ep-delete" size="14px" color="unset" class="mr-1"/>
        清空
      </a-button>
    </a-space>

    <a-table
        :data-source="sysOperLogList"
        :border="true"
        size="large"
        @selection-change="handleSelectionChange"
        row-key="operId"
        :row-selection="{
        selectedRowKeys: ids,
        onChange: (selectedRowKeys) => {
          ids = selectedRowKeys;
        }
      }">
      <a-table-column type="selection" width="55"/>
      <a-table-column data-index="operId" title="日志编号"/>
      <a-table-column data-index="title" title="模块名"/>
      <a-table-column data-index="operType" title="操作类型">
        <template #default="{record}">
          <a-tag :color="operateTypeList[record.operType].type">{{ operateTypeList[record.operType].label }}
          </a-tag>
        </template>
      </a-table-column>
      <a-table-column data-index="operName" title="操作人员"/>
      <a-table-column data-index="operIp" title="操作IP地址"/>
      <a-table-column data-index="operLocation" title="操作地址"/>
      <a-table-column data-index="status" title="操作状态">
        <template #default="{record}">
          <a-tag :color="operateLogStatusList[record.status].type">{{ operateLogStatusList[record.status].label }}
          </a-tag>
        </template>
      </a-table-column>
      <a-table-column data-index="costTime" title="操作耗时"/>
      <a-table-column data-index="operTime" title="操作日期" width="220"/>
      <a-table-column fixed="right" title="操作" width="200px">
        <template #default="{record}">
          <a-button class="ml-2" type="link" @click="handleDetail(record.operId)" v-hasPermission="['system:oper_log:detail']">详情
          </a-button>
        </template>
      </a-table-column>
    </a-table>

    <oper-detail-form ref="operDetailFormRef"/>
  </div>
</template>

<style scoped lang="scss">

</style>
